package com.woniuxy.hrms.dao.impl;

import com.woniuxy.hrms.dao.IEmployeeDao;
import com.woniuxy.hrms.entity.Employee;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @program: hrms
 * @description:
 * @create: 2024-08-01 15:31
 **/
public class IEmployeeDaolmplMen implements IEmployeeDao {
    //Employee[] employees = new Employee[10];//存放员工，定义容量
    ArrayList<Employee> list = new ArrayList<>();

    @Override
    public int add(Employee employee) {
//        employees[index] = employee;
//        index++;
        list.add(employee);
        return 1;
    }

    @Override
    public int delete(int id) {
        Iterator<Employee> iterator = list.iterator();
        //得到迭代器，指向第一条数据之前
        while (iterator.hasNext()) {//判断有没有下一条
            Employee employee = iterator.next();//如果取得该条数据
            if (employee.getId() == id) {
                iterator.remove();//删除该条数据
            }
        }
//        for (int i = 0; i < list.size(); i++) {
//            if (list.get(i).getId() == id) {
//                list.set(i, null);
//            }
//        }
        return 1;
    }

    @Override
    public int update(Employee employee) {
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getId() == employee.getId()) {
                list.set(i, employee);
            }
        }
        return 1;
    }

    @Override
    public List<Employee> queryALL() {
        return list;
    }

    @Override
    public Employee queryByld(int id) {
        Employee employee = null;
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getId() == id) {//数据不能为空，再判断两个成员id是否相等
                employee = list.get(i);//用新员工替换旧员工
            }
        }
        return employee;
    }
}
